
package com.yuan.graduate;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.OnCameraChangeListener;
import com.amap.api.maps.AMap.OnMapLongClickListener;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.PolylineOptions;
import com.yuan.application.BaseActivity;
import com.yuan.unit.AsyncDataJobHandler;
import com.yuan.unit.FileUtils;
import com.yuan.unit.ToastHelper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;

import java.io.IOException;
import java.io.StreamCorruptedException;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

public class TrailActivity extends BaseActivity implements
        OnMapLongClickListener, OnCameraChangeListener, OnClickListener {
    private MapView mapView = null;
    private AMap aMap = null;
    private Context mContext;
    private DatePicker trail_dp = null;
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what)
            {
                case GETTRAILSUCCEED:
                    updateMap();
                    break;
                case GETTRAILFILLED:
                    break;
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trail);
        mapView = (MapView) findViewById(R.id.map_view);
        mapView.onCreate(savedInstanceState);// 必须要写
        mContext = this;
        initView();
    }

    protected void updateMap() {
        // TODO Auto-generated method stub
        List<?> mList = null;
        try {
            mList = (List<?>) FileUtils.File2erializable(SERIALIZABLE_员工轨迹);
        } catch (StreamCorruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (mList == null || mList.size() == 0)
        {
            return;
        }
        int mList_size = mList.size();
        LatLng mLatLng = null;
        PolylineOptions mPolylineOptions = new PolylineOptions();
        for (int i = mList_size - 1; i >= 0; i--)
        {
            Map<?, ?> mMap = (Map<?, ?>) mList.get(i);
            double lng = Double.parseDouble(mMap.get("lng").toString());
            double lat = Double.parseDouble(mMap.get("lat").toString());
            mLatLng = new LatLng(lat, lng);
            if (lng > 1.0) {
                mPolylineOptions.add(mLatLng);
            }
        }
        aMap.clear();
        try {
            aMap.addPolyline(mPolylineOptions
                    .width(10).setDottedLine(true).geodesic(true)
                    .color(Color.argb(255, 1, 1, 1)));
            CameraUpdate cameraupdate = CameraUpdateFactory.newLatLngZoom(
                    mLatLng, 16);
            aMap.animateCamera(cameraupdate);
        } catch (IllegalStateException e)
        {
            e.printStackTrace();
            ToastHelper.getInstance().shortToast(mContext, "存在异常经纬度");
        }

    }

    private void initDataTime() {
        // TODO Auto-generated method stub
        Calendar ca = Calendar.getInstance();
        int year = ca.get(Calendar.YEAR);
        int month = ca.get(Calendar.MONTH);
        int day = ca.get(Calendar.DATE);
        trail_dp.init(year, month, day, new OnDateChangedListener() {
            @Override
            // 实现该接口 当用户点击改变日期是会被调用
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                // TODO Auto-generated method stub
                updateMap(year, monthOfYear + 1, dayOfMonth);
            }

        });
    }

    private void updateMap(int year, int monthOfYear, int dayOfMonth) {
        // TODO Auto-generated method stub

        String date = year + "-";
        if (monthOfYear < 10)
        {
            date = date + "0" + monthOfYear;
        } else
        {
            date = date + "" + monthOfYear;
        }
        if (dayOfMonth < 10)
        {
            date = date + "-0" + dayOfMonth;
        } else
        {
            date = date + "-" + dayOfMonth;
        }
        AsyncDataJobHandler.getInstance().gettrail(mHandler, date);
    }

    private void initView() {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub
        if (aMap == null) {
            aMap = mapView.getMap();
        }
        aMap.setOnMapLongClickListener(this);// 对amap添加长按地图事件监听器
        aMap.setOnCameraChangeListener(this);// 对amap添加移动地图事件监听器
        trail_dp = (DatePicker) findViewById(R.id.trail_dp);
        initDataTime();

    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onCameraChange(CameraPosition arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onCameraChangeFinish(CameraPosition arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onMapLongClick(LatLng arg0) {
        // TODO Auto-generated method stub

    }
}
